﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ShoppingEntity.UserInfo;

namespace ShoppingBll
{
    public class UserInfoBll : BaseDeleteBll<UserInfo>, IUserInfoBll
    {
        private IUserInfoDal _userInfoDal;
        private IUserInfo_RoleInfoDal _userInfo_RoleInfo;
        private IRoleInfoDal _roleInfoDal;

        public UserInfoBll(IUserInfoDal userInfoDal, IUserInfo_RoleInfoDal userInfo_RoleInfo, IRoleInfoDal roleInfoDal) : base(userInfoDal)
        {
            _userInfoDal = userInfoDal;
            _userInfo_RoleInfo = userInfo_RoleInfo;
            _roleInfoDal = roleInfoDal;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpwdMd5"></param>
        /// <param name="newpwdMd5"></param>
        /// <param name="userInfo"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool EditPassword(string oldPassword, string newPassword, UserInfo userInfo, out string msg)
        {
            //获取数据库当前登录用户的实体信息
          var entity=_userInfoDal.GetEntitiesDb().FirstOrDefault(u => userInfo.ID == u.ID && !u.IsDelete);
          
            if(entity == null)
            {
                msg = "未查询到用户信息";
                return false;
            }

            //判断新旧密码是否shuruzhengq
            if(oldPassword !=entity.PassWord)
            {
                msg = "旧密码不正确";
                return false;
            }


            //修改用户密码
            entity.PassWord = newPassword;
            if (_userInfoDal.Update(entity))
            {
                msg = "修改成功";
                return true;
            }
            else
            {
                msg = "修改失败";
                return false;
            }
        }


        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwdMD5"></param>
        /// <returns></returns>
        public UserInfo GetUserInfoByLogin(string account, string pwdMD5)
        {
            return _userInfoDal.GetEntitiesDb().Where(u => u.Account == account && u.PassWord == pwdMD5 && u.IsDelete == false).FirstOrDefault();
        }


        /// <summary>
        /// 用户页显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="userName"></param>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        public object GetUserInfoListByPage(int page, int limit, out int count, string userName, string phoneNum)
        {
            //获取数据库中用户没有删除的数据（未真实查询）
            var userInfos = _userInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);
            if (!string.IsNullOrEmpty(userName))
            {
                userInfos = userInfos.Where(u => u.UserName.Contains(userName));
            }
            if (!string.IsNullOrEmpty(phoneNum))
            {
                userInfos = userInfos.Where(u => u.PhoneNum.Contains(phoneNum));
            }

            //查询出来数据的数量
            count = userInfos.Count();

            //分页
            userInfos = userInfos.OrderBy(s => s.CreateTime).Skip((page - 1) * limit).Take(limit);

            var list = userInfos.ToList().Select(u =>
            {
                string sexStr;
                if (u.Sex == 1)
                {
                    sexStr = "男";
                }
                else
                {
                    sexStr = "女";
                }

                return new
                {
                    u.UserName,
                    u.ID,
                    u.Account,
                    u.PhoneNum,
                    u.Email,
                    u.Address,
                    Sex = sexStr,
                    u.Balance,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),

                };


            });
            return list;
        }



        /// <summary>
        /// 用户下拉选
        /// </summary>
        /// <returns></returns>
        public object GetUserInfoSelectOption()
        {
            return _userInfoDal.GetEntitiesDb().Where(u => !u.IsDelete).Select(u => new
            {
                u.ID,
                u.UserName
            }).ToList();
        }


        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="userName"></param>
        /// <param name="sex"></param>
        /// <param name="phoneNum"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(string userInfoId, string userName, int sex, string phoneNum, string address)
        {
            UserInfo userInfo = _userInfoDal.GetEntity(userInfoId);
            if (userInfo != null)
            {
                userInfo.UserName = userName;
                userInfo.Sex = sex;
                userInfo.PhoneNum = phoneNum;
                userInfo.Address = address;
                return _userInfoDal.Update(userInfo);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断登录用户的角色
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public object IsUserRole(string UserID)
        {
            object userrole = from r in _roleInfoDal.GetEntitiesDb()
                              join ur in _userInfo_RoleInfo.GetEntitiesDb().Where(u => u.UserId.Contains(UserID))
                              on r.ID equals ur.RoleID
                              select new
                              {
                                  r.RoleName
                              };
            return userrole;
        }


        /// <summary>
        /// 注册用户商家配送员审批
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddUserInfo(UserInfo entity)
        {

            if (entity.UserInfoRole == UserInfo.UserInfoRoleInfoEnum.用户)
            {
                entity.IsDelete = false;
                bool isSuccess1 = _userInfoDal.Add(entity);
                if (isSuccess1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (entity.UserInfoRole == UserInfo.UserInfoRoleInfoEnum.商家)
            {
                entity.IsDelete = true;
                bool isSuccess1 = _userInfoDal.Add(entity);
                if (isSuccess1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (entity.UserInfoRole == UserInfo.UserInfoRoleInfoEnum.配送员)
            {
                entity.IsDelete = true;
                bool isSuccess1 = _userInfoDal.Add(entity);
                if (isSuccess1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 待审批页面显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public object GetApproverUserInfoListByPage(int page, int limit, out int count, string userName, UserInfoRoleInfoEnum UserInfoRole)
        {
            //获取数据库中用户没有删除的数据（未真实查询）
            var userInfos = _userInfoDal.GetEntitiesDb().Where(u => u.IsDelete == true && u.DeleteTime == null);

            if (!string.IsNullOrEmpty(userName))
            {
                userInfos = userInfos.Where(u => u.UserName.Contains(userName));
            }

            //查询出来数据的数量
            count = userInfos.Count();

            //分页
            userInfos = userInfos.OrderBy(s => s.CreateTime).Skip((page - 1) * limit).Take(limit);

            var list = userInfos.ToList().Select(u =>
            {
                string UserInfoRole = u.UserInfoRole.ToString();

                string sexStr;
                if (u.Sex == 1)
                {
                    sexStr = "男";
                }
                else
                {
                    sexStr = "女";
                }
                return new
                {
                    u.UserName,
                    u.ID,
                    u.Account,
                    u.PhoneNum,
                    u.Email,
                    u.Address,
                    Sex = sexStr,
                    u.Balance,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    UserInfoRole
                };


            });
            return list;
        }


        /// <summary>
        /// 管理员同意审批
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool TurnApproverUserInfo(string userID)
        {
            UserInfo userInfo = _userInfoDal.GetEntity(userID);
            if (userInfo == null)
            {
                return false;
            }
            var RoleInfo = _roleInfoDal.GetEntitiesDb().Where(r => r.RoleName.Contains(userInfo.UserInfoRole.ToString())).FirstOrDefault();
            if (RoleInfo == null)
            {
                return false;
            }
            else
            {
                UserInfo_RoleInfo userInfo_RoleInfo = new UserInfo_RoleInfo()
                {
                    ID = Guid.NewGuid().ToString(),
                    UserId = userInfo.ID,
                    RoleID = RoleInfo.ID,
                    CreateTime = DateTime.Now
                };

                bool isSuccess1 = _userInfo_RoleInfo.Add(userInfo_RoleInfo);

                userInfo.IsDelete = false;
                bool isSuccess2 = _userInfoDal.Update(userInfo);

                if (isSuccess1 && isSuccess2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        /// <summary>
        /// 驳回审批
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool ConsentApproverUserInfo(string userID)
        {
            UserInfo userInfo = _userInfoDal.GetEntity(userID);
            if (userInfo == null)
            {
                return false;
            }
            var RoleInfo = _roleInfoDal.GetEntitiesDb().Where(r => r.RoleName.Contains(userInfo.UserInfoRole.ToString())).FirstOrDefault();
            if (RoleInfo == null)
            {
                return false;
            }
            else
            {
                UserInfo_RoleInfo userInfo_RoleInfo = new UserInfo_RoleInfo()
                {
                    ID = Guid.NewGuid().ToString(),
                    UserId = userInfo.ID,
                    RoleID = RoleInfo.ID,
                    CreateTime = DateTime.Now
                };

                bool isSuccess1 = _userInfo_RoleInfo.Add(userInfo_RoleInfo);

                userInfo.IsDelete = false;
                bool isSuccess2 = _userInfoDal.Update(userInfo);

                if (isSuccess1 && isSuccess2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
    }
}
